<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body>
<div class="article-body">
    <div class="article-intro">

        <h1>Java 泛型</h1>

        Java 泛型（generics）是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制，该机制允许程序员在编译时检测到非法的类型。<p></p>
        <p>泛型的本质是参数化类型，也就是说所操作的数据类型被指定为一个参数。</p>


        <blockquote>
            <p>
                假定我们有这样一个需求：写一个排序方法，能够对整型数组、字符串数组甚至其他任何类型的数组进行排序，该如何实现？
            </p>
            <p>答案是可以使用 <strong>Java 泛型</strong>。</p>
            <p>
                使用 Java 泛型的概念，我们可以写一个泛型方法来对一个对象数组排序。然后，调用该泛型方法来对整型数组、浮点数数组、字符串数组等进行排序。</p></blockquote>


        <hr><h2>
        泛型方法</h2>
        <p>
            你可以写一个泛型方法，该方法在调用时可以接收不同类型的参数。根据传递给泛型方法的参数类型，编译器适当地处理每一个方法调用。</p><p>
        下面是定义泛型方法的规则：</p>
        <ul>
            <li>
                <span id="result_box">所有</span>泛型方法声明都有一个类型参数声明部分（由尖括号分隔），该类型参数声明部分在方法返回类型之前（在下面例子中的&lt;E&gt;）。</li>
            <li>
                每一个类型参数声明部分包含一个或多个类型参数，参数间用逗号隔开。一个泛型参数，也被称为一个类型变量，是用于指定一个泛型类型名称的标识符。</li>
            <li>
                类型参数能被用来声明返回值类型，并且能作为泛型方法得到的实际参数类型的占位符。</li>
            <li>
                泛型方法体的声明和其他方法一样。注意类型参数只能代表引用型类型，不能是原始类型（像int,double,char的等）。</li>
        </ul>
        <h3>实例</h3>
        <p>
            下面的例子演示了如何使用泛型方法打印不同字符串的元素：
        </p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">GenericMethodTest</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-comment">//</span><span class="hl-comment"> 泛型方法 printArray                         </span><span class="hl-comment"></span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> &lt; </span><span class="hl-identifier">E</span><span class="hl-code"> &gt; </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">printArray</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">E</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">inputArray</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-comment">//</span><span class="hl-comment"> 输出数组元素            </span><span class="hl-comment"></span><span class="hl-code">
         </span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">E</span><span class="hl-code"> </span><span class="hl-identifier">element</span><span class="hl-code"> : </span><span class="hl-identifier">inputArray</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
            </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">%s </span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-identifier">element</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
         </span><span class="hl-brackets">}</span><span class="hl-code">
         </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">

    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">
    </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-comment">//</span><span class="hl-comment"> 创建不同类型数组： Integer, Double 和 Character</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">Integer</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">intArray</span><span class="hl-code"> = </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">2</span><span class="hl-code">, </span><span class="hl-number">3</span><span class="hl-code">, </span><span class="hl-number">4</span><span class="hl-code">, </span><span class="hl-number">5</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code">;
        </span><span class="hl-identifier">Double</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">doubleArray</span><span class="hl-code"> = </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-number">1</span><span class="hl-number">.1</span><span class="hl-code">, </span><span class="hl-number">2</span><span class="hl-number">.2</span><span class="hl-code">, </span><span class="hl-number">3</span><span class="hl-number">.3</span><span class="hl-code">, </span><span class="hl-number">4</span><span class="hl-number">.4</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code">;
        </span><span class="hl-identifier">Character</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">charArray</span><span class="hl-code"> = </span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-quotes">'</span><span class="hl-string">H</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">E</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">L</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">L</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">O</span><span class="hl-quotes">'</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code">;

        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">整型数组元素为:</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">printArray</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">intArray</span><span class="hl-code">  </span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment"> 传递一个整型数组</span><span class="hl-comment"></span><span class="hl-code">

        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-special">\n</span><span class="hl-string">双精度型数组元素为:</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">printArray</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">doubleArray</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment"> 传递一个双精度型数组</span><span class="hl-comment"></span><span class="hl-code">

        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-special">\n</span><span class="hl-string">字符型数组元素为:</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">printArray</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">charArray</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment"> 传递一个字符型数组</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div><br>
        </div>
        <p>
            编译以上代码，运行结果如下所示：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">整型数组元素为:</span><span class="pln">
</span><span class="lit">1</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="lit">3</span><span class="pln"> </span><span class="lit">4</span><span class="pln"> </span><span class="lit">5</span><span class="pln">

</span><span class="pun">双精度型数组元素为:</span><span class="pln">
</span><span class="lit">1.1</span><span class="pln"> </span><span class="lit">2.2</span><span class="pln"> </span><span class="lit">3.3</span><span class="pln"> </span><span class="lit">4.4</span><span class="pln">

</span><span class="pun">字符型数组元素为:</span><span class="pln">
H E L L O </span></pre>
        <p>
            有界的类型参数:
        </p><p>
        可能有时候，你会想限制那些被允许传递到一个类型参数的类型种类范围。例如，一个操作数字的方法可能只希望接受Number或者Number子类的实例。这就是有界类型参数的目的。
    </p><p>要声明一个有界的类型参数，首先列出类型参数的名称，后跟extends关键字，最后紧跟它的上界。</p>
        <h3>
            实例</h3>
        <p>
            下面的例子演示了"extends"如何使用在一般意义上的意思"extends"（类）或者"implements"（接口）。该例子中的泛型方法返回三个可比较对象的最大值。
        </p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">MaximumTest</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-comment">//</span><span class="hl-comment"> 比较三个值并返回最大值</span><span class="hl-comment"></span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> &lt;</span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-reserved">extends</span><span class="hl-code"> </span><span class="hl-identifier">Comparable</span><span class="hl-code">&lt;</span><span class="hl-identifier">T</span><span class="hl-code">&gt;&gt; </span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">maximum</span><span class="hl-brackets">(</span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span class="hl-code">, </span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">y</span><span class="hl-code">, </span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">z</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">max</span><span class="hl-code"> = </span><span class="hl-identifier">x</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment"> 假设x是初始最大值</span><span class="hl-comment"></span><span class="hl-code">
      </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">y</span><span class="hl-code">.</span><span class="hl-identifier">compareTo</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">max</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> &gt; </span><span class="hl-number">0</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
         </span><span class="hl-identifier">max</span><span class="hl-code"> = </span><span class="hl-identifier">y</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment">y 更大</span><span class="hl-comment"></span><span class="hl-code">
      </span><span class="hl-brackets">}</span><span class="hl-code">
      </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">z</span><span class="hl-code">.</span><span class="hl-identifier">compareTo</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">max</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> &gt; </span><span class="hl-number">0</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
         </span><span class="hl-identifier">max</span><span class="hl-code"> = </span><span class="hl-identifier">z</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment"> 现在 z 更大           </span><span class="hl-comment"></span><span class="hl-code">
      </span><span class="hl-brackets">}</span><span class="hl-code">
      </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">max</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment"> 返回最大对象</span><span class="hl-comment"></span><span class="hl-code">
   </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">%d, %d 和 %d 中最大的数为 %d</span><span class="hl-special">\n</span><span class="hl-special">\n</span><span class="hl-quotes">"</span><span class="hl-code">,
                   </span><span class="hl-number">3</span><span class="hl-code">, </span><span class="hl-number">4</span><span class="hl-code">, </span><span class="hl-number">5</span><span class="hl-code">, </span><span class="hl-identifier">maximum</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-number">3</span><span class="hl-code">, </span><span class="hl-number">4</span><span class="hl-code">, </span><span class="hl-number">5</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;

      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">%.1f, %.1f 和 %.1f 中最大的数为 %.1f</span><span class="hl-special">\n</span><span class="hl-special">\n</span><span class="hl-quotes">"</span><span class="hl-code">,
                   </span><span class="hl-number">6</span><span class="hl-number">.6</span><span class="hl-code">, </span><span class="hl-number">8</span><span class="hl-number">.8</span><span class="hl-code">, </span><span class="hl-number">7</span><span class="hl-number">.7</span><span class="hl-code">, </span><span class="hl-identifier">maximum</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-number">6</span><span class="hl-number">.6</span><span class="hl-code">, </span><span class="hl-number">8</span><span class="hl-number">.8</span><span class="hl-code">, </span><span class="hl-number">7</span><span class="hl-number">.7</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;

      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">%s, %s 和 %s 中最大的数为 %s</span><span class="hl-special">\n</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-quotes">"</span><span class="hl-string">pear</span><span class="hl-quotes">"</span><span class="hl-code">,
         </span><span class="hl-quotes">"</span><span class="hl-string">apple</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">orange</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-identifier">maximum</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-quotes">"</span><span class="hl-string">pear</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">apple</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">orange</span><span class="hl-quotes">"</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div><br>
        </div>
        <p>
            编译以上代码，运行结果如下所示：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="lit">3</span><span class="pun">,</span><span class="pln"> </span><span class="lit">4</span><span class="pln"> </span><span class="pun">和</span><span class="pln"> </span><span class="lit">5</span><span class="pln"> </span><span class="pun">中最大的数为</span><span class="pln"> </span><span class="lit">5</span><span class="pln">

</span><span class="lit">6.6</span><span class="pun">,</span><span class="pln"> </span><span class="lit">8.8</span><span class="pln"> </span><span class="pun">和</span><span class="pln"> </span><span class="lit">7.7</span><span class="pln"> </span><span class="pun">中最大的数为</span><span class="pln"> </span><span class="lit">8.8</span><span class="pln">

pear</span><span class="pun">,</span><span class="pln"> apple </span><span class="pun">和</span><span class="pln"> orange </span><span class="pun">中最大的数为</span><span class="pln"> pear</span></pre>
        <hr>
        <h2>
            泛型类</h2>
        <p>
            泛型类的声明和非泛型类的声明类似，除了在类名后面添加了类型参数声明部分。
        </p><p>和泛型方法一样，泛型类的类型参数声明部分也包含一个或多个类型参数，参数间用逗号隔开。一个泛型参数，也被称为一个类型变量，是用于指定一个泛型类型名称的标识符。因为他们接受一个或多个参数，这些类被称为参数化的类或参数化的类型。
    </p><h3>实例</h3>
        <p>

            如下实例演示了我们如何定义一个泛型类:</p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Box</span><span class="hl-code">&lt;</span><span class="hl-identifier">T</span><span class="hl-code">&gt; </span><span class="hl-brackets">{</span><span class="hl-code">

  </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">t</span><span class="hl-code">;

  </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">t</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">t</span><span class="hl-code"> = </span><span class="hl-identifier">t</span><span class="hl-code">;
  </span><span class="hl-brackets">}</span><span class="hl-code">

  </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">T</span><span class="hl-code"> </span><span class="hl-identifier">get</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">t</span><span class="hl-code">;
  </span><span class="hl-brackets">}</span><span class="hl-code">

  </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">Box</span><span class="hl-code">&lt;</span><span class="hl-identifier">Integer</span><span class="hl-code">&gt; </span><span class="hl-identifier">integerBox</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Box</span><span class="hl-code">&lt;</span><span class="hl-identifier">Integer</span><span class="hl-code">&gt;</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">Box</span><span class="hl-code">&lt;</span><span class="hl-identifier">String</span><span class="hl-code">&gt; </span><span class="hl-identifier">stringBox</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Box</span><span class="hl-code">&lt;</span><span class="hl-identifier">String</span><span class="hl-code">&gt;</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;

    </span><span class="hl-identifier">integerBox</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Integer</span><span class="hl-brackets">(</span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">stringBox</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">String</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">菜鸟教程</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;

    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">整型值为 :%d</span><span class="hl-special">\n</span><span class="hl-special">\n</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-identifier">integerBox</span><span class="hl-code">.</span><span class="hl-identifier">get</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">printf</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">字符串为 :%s</span><span class="hl-special">\n</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-identifier">stringBox</span><span class="hl-code">.</span><span class="hl-identifier">get</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;
  </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div><br>
        </div>
        <p>
            编译以上代码，运行结果如下所示：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">整型值为</span><span class="pln"> </span><span class="pun">:</span><span class="lit">10</span><span class="pln">

</span><span class="pun">字符串为</span><span class="pln"> </span><span class="pun">:菜鸟教程</span></pre>
        <hr>
        <h2>类型通配符</h2>
        <p>1、类型通配符一般是使用?代替具体的类型参数。例如 <strong>List&lt;?&gt;</strong> 在逻辑上是<strong>List&lt;String&gt;,List&lt;Integer&gt;</strong> 等所有List&lt;具体类型实参&gt;的父类。
        </p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">GenericTest</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">

    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">List</span><span class="hl-code">&lt;</span><span class="hl-identifier">String</span><span class="hl-code">&gt; </span><span class="hl-identifier">name</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">ArrayList</span><span class="hl-code">&lt;</span><span class="hl-identifier">String</span><span class="hl-code">&gt;</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">List</span><span class="hl-code">&lt;</span><span class="hl-identifier">Integer</span><span class="hl-code">&gt; </span><span class="hl-identifier">age</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">ArrayList</span><span class="hl-code">&lt;</span><span class="hl-identifier">Integer</span><span class="hl-code">&gt;</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">List</span><span class="hl-code">&lt;</span><span class="hl-identifier">Number</span><span class="hl-code">&gt; </span><span class="hl-identifier">number</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">ArrayList</span><span class="hl-code">&lt;</span><span class="hl-identifier">Number</span><span class="hl-code">&gt;</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;

        </span><span class="hl-identifier">name</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">icon</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">age</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-number">18</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">number</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-number">314</span><span class="hl-brackets">)</span><span class="hl-code">;

        </span><span class="hl-identifier">getData</span><span class="hl-brackets">(</span><span class="hl-identifier">name</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">getData</span><span class="hl-brackets">(</span><span class="hl-identifier">age</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">getData</span><span class="hl-brackets">(</span><span class="hl-identifier">number</span><span class="hl-brackets">)</span><span class="hl-code">;

   </span><span class="hl-brackets">}</span><span class="hl-code">

   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">getData</span><span class="hl-brackets">(</span><span class="hl-identifier">List</span><span class="hl-code">&lt;?&gt; </span><span class="hl-identifier">data</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">data :</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">data</span><span class="hl-code">.</span><span class="hl-identifier">get</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div><br>
        </div>
        <p>输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">data </span><span class="pun">:</span><span class="pln">icon
data </span><span class="pun">:</span><span class="lit">18</span><span class="pln">
data </span><span class="pun">:</span><span class="lit">314</span></pre>
        <p><strong>解析：</strong>

            因为getData()方法的参数是List<!--?-->类型的，所以name，age，number都可以作为这个方法的实参，这就是通配符的作用
        </p>

        <p>2、类型通配符上限通过形如List<!--? extends Number-->来定义，如此定义就是通配符泛型值接受Number及其下层子类类型。

        </p>
        <div class="example">
            <div class="example">实例</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">GenericTest</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">

    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">List</span><span class="hl-code">&lt;</span><span class="hl-identifier">String</span><span class="hl-code">&gt; </span><span class="hl-identifier">name</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">ArrayList</span><span class="hl-code">&lt;</span><span class="hl-identifier">String</span><span class="hl-code">&gt;</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">List</span><span class="hl-code">&lt;</span><span class="hl-identifier">Integer</span><span class="hl-code">&gt; </span><span class="hl-identifier">age</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">ArrayList</span><span class="hl-code">&lt;</span><span class="hl-identifier">Integer</span><span class="hl-code">&gt;</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">List</span><span class="hl-code">&lt;</span><span class="hl-identifier">Number</span><span class="hl-code">&gt; </span><span class="hl-identifier">number</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">ArrayList</span><span class="hl-code">&lt;</span><span class="hl-identifier">Number</span><span class="hl-code">&gt;</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;

        </span><span class="hl-identifier">name</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">icon</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">age</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-number">18</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">number</span><span class="hl-code">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-number">314</span><span class="hl-brackets">)</span><span class="hl-code">;

        </span><span class="hl-comment">//</span><span class="hl-comment">getUperNumber(name);//1</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">getUperNumber</span><span class="hl-brackets">(</span><span class="hl-identifier">age</span><span class="hl-brackets">)</span><span class="hl-code">;</span><span class="hl-comment">//</span><span class="hl-comment">2</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">getUperNumber</span><span class="hl-brackets">(</span><span class="hl-identifier">number</span><span class="hl-brackets">)</span><span class="hl-code">;</span><span class="hl-comment">//</span><span class="hl-comment">3</span><span class="hl-comment"></span><span class="hl-code">

   </span><span class="hl-brackets">}</span><span class="hl-code">

   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">getData</span><span class="hl-brackets">(</span><span class="hl-identifier">List</span><span class="hl-code">&lt;?&gt; </span><span class="hl-identifier">data</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">data :</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">data</span><span class="hl-code">.</span><span class="hl-identifier">get</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">

   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">getUperNumber</span><span class="hl-brackets">(</span><span class="hl-identifier">List</span><span class="hl-code">&lt;? </span><span class="hl-reserved">extends</span><span class="hl-code"> </span><span class="hl-identifier">Number</span><span class="hl-code">&gt; </span><span class="hl-identifier">data</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
          </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">data :</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">data</span><span class="hl-code">.</span><span class="hl-identifier">get</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;
       </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div><br>
        </div>
        <p>输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">data </span><span class="pun">:</span><span class="lit">18</span><span class="pln">
data </span><span class="pun">:</span><span class="lit">314</span></pre>
        <p><strong>解析： </strong>

            在(//1)处会出现错误，因为getUperNumber()方法中的参数已经限定了参数泛型上限为Number，所以泛型为String是不在这个范围之内，所以会报错
        </p>
        <p>3、类型通配符下限通过形如 <strong>List&lt;? super Number&gt;</strong>来定义，表示类型只能接受Number及其三层父类类型，如Objec类型的实例。</p>

    </div>
</div>
</body>
</html>
